package com.gupaoedu.baifc.singleton;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * projectName: baifc_singleton
 * packageName: com.gupaoedu.baifc.singleton
 * Created: 2019/3/12.
 * Auther: baifc
 * Description: 容器式单例
 */
public class ContainerSingleton {

    private static Map<String, Object> map = new ConcurrentHashMap<>();

    public Object getBean(String className) {

        Object obj = null;
        if (map.containsKey(className)) {
            obj = map.get(className);
        } else {
            try {
                obj = Class.forName(className).newInstance();
                map.put(className, obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return obj;
    }
}
